app.controller('bpmAddAttachmentModalController', ['$scope', '$uibModalInstance', 'data', 'httpWrapService', 'dialogs', 'FileUploader', function ($scope, $uibModalInstance, data, httpWrapService, dialogs, FileUploader) {

    $scope.data = {};
    $scope.uploadType = 'file';

    var uploader = $scope.uploader = new FileUploader({
        url : 'bpm/attachment/new/file.do',
        headers: {
            'Accept': 'application/json, text/plain, */*'
        }
    });

    $scope.changeType = function(index) {
        if(index == '1') {
            $scope.uploadType = 'url';
        } else {
            $scope.uploadType = 'file';
        }
    }
    $scope.upload = function() {
        if($scope.uploadType == 'file') {
            uploader.uploadAll();
        } else {
            httpWrapService.post('bpm/attachment/new/url.do', {
                taskId: data.taskId,
                processInstanceId: data.processInstanceId,
                attachmentName: $scope.data.attachmentName,
                attachmentDescription: $scope.data.attachmentDescription,
                url: $scope.data.url
            }).then(function (res) {
                $uibModalInstance.close();
            }, function (res) {
                dialogs.error('错误', res.data.msg, {size: 'md'});
            });
        }
    }

    uploader.onBeforeUploadItem = function(item) {
        item.formData = [
            {
                taskId: data.taskId
            },
            {
                processInstanceId: data.processInstanceId
            },
            {
                attachmentName: $scope.data.attachmentName
            },
            {
                attachmentDescription: $scope.data.attachmentDescription
            }
        ]
        console.info('onBeforeUploadItem', item);
    };

    uploader.onSuccessItem = function(fileItem, response, status, headers) {
        if(!response.success) {
            console.info('上传处理失败！');
            dialogs.error('错误', '['+fileItem.file.name+'] ' + response.msg, {size: 'md'});
            fileItem.remove();
        } else {
            $uibModalInstance.close();
        }
        //console.info('onSuccessItem', fileItem, response, status, headers);
    };

    $scope.cancel = function () {
        $uibModalInstance.dismiss('Canceled');
    }; // end cancel

}]); // end controller(customDialogCtrl)


